package views
{
	import assists.log.Log;
	
	import com.greensock.TweenLite;
	
	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import inerface.IPanel;
	
	import main.ClientConstants;
	import main.ViewSubBase;
	
	/**
	 * ...
	 * @author clouree
	 */
	public class ViewLayout extends Sprite 
	{
		private var _showViewList:Dictionary = new Dictionary();
		
		private var showViewClassList:Array;
		private var baseLayerList:Array;
		private var guideLayerList:Array;
		private var alertLayerList:Array;
		
		private var singelPanel:Array;
		private var homePanel:Array;
		
		private var currentView:ViewSubBase;
		private var backgroundView:ViewSubBase;
		private var guideView:GuideView;
		private var alertView:ViewSubBase;
		
		private var baseLayer:Sprite;
		private var panelLayer:Sprite;
		private var guideLayer:Sprite;
		private var alertLayer:Sprite;
		
		private var _timerOut:Timer;
		private var _Stage:Stage;
		
		private const LAYER_BASE:int = 1;
		private const LAYER_PANEL:int = 2;
		private const LAYER_GUIDE:int = 3;
		private const LAYER_ALERT:int = 4;
		
		private var closeFunc:Function;
		
		public function ViewLayout(stage:Stage)
		{
			_Stage = stage;
			baseLayerList = [BackgroundView];
			showViewClassList = [];
			guideLayerList = [GuideView];
			alertLayerList = [NewFunctionPreviewView,LoadingWaitView,AlertView];
			singelPanel = [ViewSpotView,EatingView,ScenicShopView,HotelView];                                     //除主界面之外的四个一级不共存界面
			homePanel = [HomeView]                                                            //主界面;
			
			baseLayer = new Sprite;
			panelLayer = new Sprite;
			guideLayer = new Sprite;
			alertLayer = new Sprite;
			
			this.addChild(baseLayer);
			this.addChild(panelLayer);
			this.addChild(guideLayer);
			this.addChild(alertLayer);
			
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownHandler);       //注册键盘事件
		}
		
		protected function onKeyDownHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.BACK){
				if(showViewClassList.length <= 1){
					//					NativeApplication.nativeApplication.exit();
					event.preventDefault();
					
					if(closeFunc is Function){
						closeFunc();
					}
				}else{
					closeCurrent();
					event.preventDefault();
				}
			}else{
				return;
			}
		}
		
		public function closeCurrent():void
		{
			if(showViewClassList.length <= 1){
				checkBackState();
			}else{
				backChangeViewList();
			}
		}
		
		private function checkBackState():void
		{
			if(currentView is homePanel[0]){
				hideBack();
				return;
			}
			
			for(var i:int = 0;i < singelPanel.length;i++){
				if(currentView is singelPanel[i]){
					hideBack();
					return;
				}
			}
			
			showBack();
		}
		
		private function showBack():void
		{
			guideView.showOrHide = true;
		}
		
		private function hideBack():void
		{
			guideView.showOrHide = false;
		}
		
		private function checkLocation(typeClass:ViewSubBase):int
		{
			var count:int = 0;
			for(var i:int = 0;i < baseLayerList.length;i++){
				if( typeClass is baseLayerList[i]){
					count = LAYER_BASE;
				}
			}
			if(count != LAYER_BASE){
				for(var j:int = 0;j < alertLayerList.length;j++){
					if(typeClass is alertLayerList[j]){
						count = LAYER_ALERT;
					}
				}
			}
			if(count != LAYER_BASE && count != LAYER_ALERT){
				for(var k:int = 0;k < guideLayerList.length;k++){
					if(typeClass is guideLayerList[k]){
						count = LAYER_GUIDE;
					}
				}
			}
			if(count != LAYER_BASE && count != LAYER_ALERT && count != LAYER_GUIDE){
				count = LAYER_PANEL;
			}
			
			return count;
		}
		
		public function addView(typeClass:ViewSubBase,container:IPanel):void
		{
			var level:int = checkLocation(typeClass);
			if(level == LAYER_PANEL){
				if(currentView && currentView == typeClass){
					return;
				}
				changeViewList(typeClass);
				_showViewList[typeClass] = container;
				centerCurrent();
				
				if(currentView is HomeView){
					GuideView(guideView).changeState(true);
				}else{
					GuideView(guideView).changeState(false);
				}	
				
				return;
			}
			if(level == LAYER_BASE){
				backgroundView = typeClass;
				_showViewList[typeClass] = container;
				baseLayer.addChild(_showViewList[backgroundView]);
				return;
			}
			if(level == LAYER_ALERT){
				alertView = typeClass;
				_showViewList[typeClass] = container;
				alertLayer.addChild(_showViewList[alertView]);
				return;
			}
			if(level == LAYER_GUIDE){
				guideView = typeClass as GuideView;
				_showViewList[typeClass] = container;
				guideLayer.addChild(_showViewList[guideView]);
				return;
			}
		}
		
		private function centerCurrent():void
		{
			panelLayer.addChild(_showViewList[currentView]);
			_showViewList[currentView].x = 0;
		}
		
		public function closeView(typeClass:ViewSubBase):void
		{
			if(typeClass != null){
				var count:int = checkLocation(typeClass);
				if(count == LAYER_PANEL){	
					if(_showViewList[typeClass].parent && _showViewList[typeClass].parent == panelLayer){
						panelLayer.removeChild(_showViewList[typeClass]);
					}else{
						//					NativeApplication.nativeApplication.exit();
					}
				}else if(count == LAYER_BASE){
					baseLayer.removeChild(_showViewList[typeClass]);
				}else if(count == LAYER_ALERT){
					if(alertLayer.contains(_showViewList[typeClass]))
					{
					alertLayer.removeChild(_showViewList[typeClass]);
					}
					typeClass.clear();
					alertView = null;
				}else if(count == LAYER_GUIDE){
					guideLayer.removeChild(_showViewList[typeClass]);
				}
			}
		}
		
		private function clearView(viewType:ViewSubBase):void
		{
			var count:int = checkLocation(viewType);
			if(count == LAYER_PANEL){
				closeView(viewType);
				viewType.clear();
			}
		}
		
		private function changeViewList(newView:ViewSubBase = null):void
		{
			//			clearAllPanel();            //全清除
			if(currentView){
				clearView(currentView);
			}
			if(newView){
				currentView = newView;
			}
			
			if(currentView is homePanel[0]){
				showViewClassList = [currentView];
			}else{
				checkExist();
				var single:Boolean = isSingle();
				if(single == true){
					var mainView:ViewSubBase = showViewClassList.shift();
					showViewClassList = [mainView,currentView];
				}else{
					showViewClassList.push(currentView);
				}
			}
			
			checkBackState();
			
			trace("我勒个擦" + showViewClassList)
		}
		
		private function checkExist():void
		{
			for(var i:int = 0;i < showViewClassList.length;i++){
				if(currentView == showViewClassList[i]){
					showViewClassList.splice(i,1);
					break;
				}
			}
		}
		
		private function backChangeViewList():void
		{
			if(showViewClassList.length > 1){
				
				if(currentView is ViewSpotDetailView)
				{
					ClientConstants.SLIDE_POS.memory = true;
				}
				clearView(currentView);
				currentView = null;
				var preView:ViewSubBase = showViewClassList[showViewClassList.length - 2];
				showViewClassList.pop();
				showViewClassList.pop();
				
				preView.show();
			}
		}
		
		private function clearAllPanel():void
		{
			for(var i:int = 0;i < showViewClassList.length;i++){
				clearView(showViewClassList[i]);
			}
		}
		
		private function isSingle():Boolean
		{
			var bool:Boolean = false;
			for(var k:int = 0;k < singelPanel.length;k++){
				if(currentView is singelPanel[k]){
					bool = true;
					break;
				}
			}
			
			if(bool == true){
				for(var i:int = 0;i < showViewClassList.length;i++){
					for(var j:int = 0;j < singelPanel.length;j++){
						if(showViewClassList[i] is singelPanel[j]){ //showViewClassList[i] != currentView && 
							showViewClassList.splice(i,1);
						}
					}
				}
				return true;
			}
			
			return false;
		}
		
		public function instage(viewName:ViewSubBase):Boolean
		{
			if(currentView == viewName || backgroundView == viewName || alertView == viewName || guideView == viewName){
				return true;
			}
			return false;
		}
		
		public function get currentShow():ViewSubBase
		{
			return currentView;
		}
		
		public override function get stage():Stage
		{
			return _Stage;
		}
		
		public function set closeFunction(func:Function):void
		{
			closeFunc = func;
		}
	}
}